/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.data.exchange.runner.master;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author dell
 */
public class ParserFastJson {

    static Logger logger = Logger.getLogger(ParserFastJson.class);

    //record_list
    public static List<Map<String, String>> getContentByType(String jsonString, String type) {
        List<Map<String, String>> mapArray = new ArrayList<Map<String, String>>();
        try {
            JSONObject object = JSONObject.parseObject(jsonString);
            JSONArray dataArray = object.getJSONArray(type);
            if (dataArray != null) {
                for (int i = 0; i < dataArray.size(); i++) {
                    JSONObject jsonObject = dataArray.getJSONObject(i);
                    Map<String, String> map = new HashMap<String, String>();
                    for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                        String key = entry.getKey();
                        String value = (String) (entry.getValue() + "");
                        map.put(key, value);
                    }
                    mapArray.add(map);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return mapArray;
    }

    public static HashMap<String, String> getContentMap(String jsonString) {
        HashMap<String, String> map = new HashMap();
        try {
            JSONObject jsonObject = JSONObject.parseObject(jsonString);
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                String key = entry.getKey();
                String value = (String) (entry.getValue() + "");
                map.put(key, value);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return map;
    }

    public static boolean returnError(String returnContent) {
        boolean statusFlag = false;
        JSONObject jsonObject = JSONObject.parseObject(returnContent);
        String status = jsonObject.getString("status");
        if ("error".equals(status)) {
            statusFlag = true;
        }
        return statusFlag;
    }

    public static String requestMetadata(List<String> urls, HashMap paramMap) {
        JettyClient jettyClient = new JettyClient();
        int trynum = 0;
        String returnStr = "";
        while (trynum < 3) {
            trynum++;
            String url = getRandomUrl(urls);
            logger.info(paramMap.toString() + " send to " + url);
            returnStr = jettyClient.postSendReturnContent(url, paramMap);
            logger.info("send finished, return value:" + returnStr);
            boolean queryTaskFlag = returnError(returnStr);
            if (!queryTaskFlag) {
                break;
            } else {
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                }
            }
            if (trynum == 3) {
                logger.error("request metadata failed,cause " + returnStr);
            }
        }
        return returnStr;
    }

    public static String getRandomUrl(List<String> urls) {
        int length = urls.size();
        return urls.get((int) (Math.random() * length));
    }
}
